<template>
  <div class="shopping-cat h-100">
    <mt-header title="购物车" fixed>
      <span slot="left" class="h-right" @click="editor" v-if="shoppingCat.length">编辑</span>
      <span slot="right" class="h-right" @click="delShoppingCat()" v-if="shoppingCat.length"><i class="icon-bin"></i>清空</span>
    </mt-header>
    <div class="header-h"></div>
    <shopping-cat-list
        :list="shoppingCat"
        :isLoading="isLoading"
        :isEditor="isEditor"
        @selectShoppingCat="selectShoppingCat"
        @delShoppingCat="delShoppingCat"
        @addGoods="addGoods"
        @desCartGoods="desCartGoods">
    </shopping-cat-list>
    <template v-if="shoppingCat.length">
    <div class="balance-h"></div>
    <div class="balance">
      <div class="left">
        <div class="select-all">
          <i class="icon"
             @click="selectAll()"
             :class="{'icon-checkmark':isALLSelect === true,'icon-checkmark2':isALLSelect === false}"></i> 全选
        </div>
        <div class="price-all">合计:￥<em>{{priceAll}}</em></div>
      </div>
      <div class="right">
        <mt-button type="default" class="submit" @click="submit()" :disabled='submitStatus'>去结算</mt-button>
      </div>
    </div>
    </template>
    <loading v-if="isLoading"></loading>
  </div>
</template>

<script>
  import { mapActions, mapGetters } from 'vuex';
  import loading from '@/components/loading/loading';
  import shoppingCatList from '@/views/ShoppingCat/shoppingCatList';
  import cartControl from '@/components/cart-control/cart-control';
  export default {
  	name:'keepAlive',
    data() {
      return {
        isLoading: true,
        isEditor: false,
      }
    },
    computed: {
      ...mapGetters([
        'shoppingCat'
      ]),
      isALLSelect() {
        let num = 0;
        this.shoppingCat.forEach( item => {
          if(item.select === true) num++
        });
        if (this.shoppingCat.length === num) return true;
        return false;
      },
      priceAll() {
        let p = 0;
        this.shoppingCat.forEach( item => {
          if(item.select === true) {
            p += Number(item.price) * Number(item.num);
          }
        });
        return p;
      },
      submitStatus() {
        if(this.priceAll <= 0){
          return true;
        }
        return false;
      }
    },
    methods: {
      ...mapActions({
        getShoppingCat:'shoppingCat',
        addShoppingCat: 'addShoppingCat',
        delShoppingCat: 'delShoppingCat',
        selectShoppingCat: 'selectShoppingCat'
      }),
      selectAll(){
        let p = this.isALLSelect? -1 : 0;
        this.selectShoppingCat(p);
      },
      addGoods(id) {
        this.addShoppingCat({id:id});
      },
      desCartGoods (id) {
        this.delShoppingCat({id:id,status:1});
      },
      editor() {
        this.isEditor = !this.isEditor;
      },
//      add() {
//        this.addShoppingCat({id:6,title:'自己添加的',img:'https://img.alicdn.com/imgextra/i3/3121904393/O1CN011iK2NzMUp9Tvx4u_!!0-item_pic.jpg_310x310.jpg',info:'xl码',num:1,price:22,select:true}).then((rs)=>{
//          console.log(this.isSelect);
//        });
//      },
      submit() {

      }
    },
    activated(){
      this.getShoppingCat().then((rs)=>{
        this.isLoading = false;
      })
    },
    components: {
      loading,
      cartControl,
      shoppingCatList
    }
  }
</script>

<style scoped lang="less">
  @import '../../common/less/variable';
  .shopping-cat{

    .header-h{
      height: 40px;
    }
    .mint-header{
      background-color: @color-theme;
      font-size: @font-size-medium-x;
      .h-right{
        font-size: 14px;
        .i{
          vertical-align: middle;
          display: inline-block;
        }
      }
    }
    .balance-h{
      height: 104px;
    }
    .balance{
      padding: 0 15px;
      height: 50px;
      position: fixed;
      left: 0;
      width: 100%;
      bottom: 53px;
      background-color:#ffeefc;
      display: flex;
      color:@color-theme;
      justify-content: space-between;
      align-items:center;
      .left{
        display: flex;
        .select-all{
          margin-top: 1px;
          width: 80px;
          font-weight: bold;
          font-size: @font-size-medium;
          .icon{
            display: inline-block;
          }
        }
        .price-all{

          font-size: @font-size-medium-x;
          flex: 1;
          em{
            font-weight: bold;
            font-style: normal;
            font-size: @font-size-large;
          }
        }
      }
      .right{
        margin-right: 15px;
        width: 100px;
        .submit{
          width: 100px;
          height: 50px;
          border-radius: 0;
          box-shadow: none;
          font-size: @font-size-medium-x;
          background-color: @color-theme;
          color:@color-text-w;
          font-weight: bold;
        }
      }
    }
    .loading{
      margin-top: 180px;
    }
  }
</style>
